package UIFree.controls.scrollbar
{
	import UIFree.controls.uif_scrollbar;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_scrollHandle extends uif_ecomponent {
		private var _backPos: Number;
		private var _sx: Number;
		
		public function uif_scrollHandle(parent: uif_scrollbar, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass: Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_backPos = -1;
			addEvent(MouseEvent.MOUSE_DOWN, doDown);
			addEvent(MouseEvent.MOUSE_MOVE, doMove, appstage);
			addEvent(MouseEvent.MOUSE_UP, doUp, appstage);
		}
		
		protected function doDown(e: MouseEvent): void {
			if (!disable) {
				_backPos = parent.mouseX;
				_sx		 = x;
			}
		}
		
		protected function doMove(e: MouseEvent): void {
			if (!disable && (_backPos > -1)) {
				var pr: Rectangle = (parent as uif_ecomponent).getInner();
				var ras: Number = parent.mouseX - _backPos;
				if (ras > 0) {
					if (x + ras + width < pr.x + pr.width) _sx += ras;
					else _sx = pr.x + pr.width - width;
				} else {
					if (x + ras > pr.x) _sx += ras;
					else _sx = pr.x;
				}
				_backPos = parent.mouseX;
				parent.dispatchEvent(new Event(uif_scrollbar.SCROLLING));
			}
		}
		
		public function get setX(): Number {
			return _sx;
		}
		
		protected function doUp(e: MouseEvent): void {
			if (_backPos > -1) _backPos = -1;
		}
		
		override protected function paint(a_styleClass: String, a_method: String = ''): void {
			var r: Rectangle = getBorder();
			var w2: Number = r.width / 2;
			begin2(style);
			this.graphics.moveTo(r.x, r.y);
			this.graphics.lineTo(r.x + r.width, r.y);
			this.graphics.lineTo(r.x + r.width, r.y + r.height - w2);
			this.graphics.lineTo(r.x + w2, r.y + r.height);
			this.graphics.lineTo(r.x, r.y + r.height - w2);
			this.graphics.lineTo(r.x, r.y);
			if (style.fill_color) this.graphics.endFill();
		}
	}
}